package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/7/19
 * Time: 00:10
 */
public class AnalyzingBook {
    public void storeObject(Book[] book,String filename) throws IOException {
        int bookUseSize = 0;
        for (Book value : book) {
            if (value != null) {
                bookUseSize++;
            }
        }
        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < bookUseSize; i++) {
            if (book[i] != null) {
                jsonArray.append(book[i].toJSON());
                if (i != bookUseSize - 1) {
                    jsonArray.append("\n");
                }
            }
        }
        //存入文件
        FileUtils.writeFile(jsonArray.toString(),filename);
    }
    public Book[] loadObject(String filename) throws IOException {
        //1.读取文件
        String content = FileUtils.readFile(filename);
        if (content == null ||  content.isEmpty()) {
            return null;
        }
        //2.使用\n作为分隔符
        String[] bookJsonStrings = content.split("\n");
        //3.蒋对应字符包装为图书对象
        Book[] bookList = new Book[bookJsonStrings.length];
        for (int i = 0; i < bookJsonStrings.length; i++) {
            Book book = parseBooKJson(bookJsonStrings[i]);
            bookList[i] = book;
        }
        return bookList;
    }
    public Book parseBooKJson(String json){
        String[] ret = json.split(",");
        int bookId = Integer.parseInt(ret[0]);
        String title = ret[1];
        String author = ret[2];
        String category = ret[3];
        int publishYear = Integer.parseInt(ret[4]);
        boolean isBorrowed = Boolean.parseBoolean(ret[5]);
        int borrowCount = Integer.parseInt(ret[6]);
        LocalDate shelfDate = LocalDate.parse(ret[7]);
        if (title != null && author != null && category != null) {
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBorrow(isBorrowed);
            book.setBorrowCount(borrowCount);
            book.setBookId(bookId);
            return book;
        }
        return null;
    }

//    public static void main(String[] args) {
//        Book[] books = new Book[4];
//        books[0] = new Book("java", "gaobo", "编程", 1994, LocalDate.of(2023, 9, 24));
//        books[1] = new Book("mysql", "lisi", "编程", 1999, LocalDate.of(2024, 2, 10));
//        books[2] = new Book("php", "gaobo", "编程", 2020, LocalDate.of(2023, 9, 23));
//        books[3] = new Book("西游记", "吴承恩", "⼩说", 2024, LocalDate.of(2023, 9, 23));
//        AnalyzingBook analyzingBook = new AnalyzingBook();
//        try {
//            analyzingBook.storeObject(books, Constant.ALL_BOOK_FILE_NAME);
//            Book[] ret = analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
//            for (Book book : ret) {
//                System.out.println(book);
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
